package trabalho.pkg3.sd.cliente;

/*

Implementa funções próprias para utilização do JXTA
Obtida como parte dos exemplos do Practical Jxta II

*/

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import net.jxta.document.Attributable;
import net.jxta.document.Attribute;
import net.jxta.document.Element;
import net.jxta.document.StructuredDocument;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.Message.ElementIterator;
import net.jxta.endpoint.MessageElement;
import net.jxta.peer.PeerID;
import net.jxta.platform.NetworkConfigurator;
import net.jxta.platform.NetworkManager;
import net.jxta.rendezvous.RendezVousService;


public class Ferramentas {

    public Ferramentas() {
    }

    public static void popConnectedRendezvous(RendezVousService TheRendezVous, String Name) {

        List<PeerID> TheList = TheRendezVous.getLocalRendezVousView();
        Iterator<PeerID> Iter = TheList.iterator();
        int Count = 0;

        while (Iter.hasNext()) {

            Count = Count + 1;

            PopInformationMessage(Name, "Connected to rendezvous:\n\n"
                    + Iter.next().toString());

        }

        if (Count == 0) {

            PopInformationMessage(Name, "No rendezvous connected to this rendezvous!");

        }

    }

    public static void popConnectedPeers(RendezVousService TheRendezVous, String Name) {

        List<PeerID> TheList = TheRendezVous.getLocalRendezVousView();
        Iterator<PeerID> Iter = TheList.iterator();
        int Count = 0;

        while (Iter.hasNext()) {

            Count = Count + 1;

            PopInformationMessage(Name, "Peer connected to this rendezvous:\n\n"
                    + Iter.next().toString());

        }

        if (Count == 0) {

            PopInformationMessage(Name, "No peers connected to this rendezvous!");

        }

    }

    public static void CheckForRendezVousSeedAddition(String Name, String TheSeed, NetworkConfigurator TheNC) {

            URI LocalSeedingRendezVousURI = URI.create(TheSeed);
            TheNC.addSeedRendezvous(LocalSeedingRendezVousURI);
    }
    
    public static void PopInformationMessage(String Name, String Message) {

        System.out.println(Message + " " + Name);

    }

    public static void PopErrorMessage(String Name, String Message) {

        System.out.println(Message + " " + Name);

    }

    public static void PopWarningMessage(String Name, String Message) {

        System.out.println(Message + " " + Name);

    }

    public static void CheckForExistingConfigurationDeletion(String Name, File ConfigurationFile) throws IOException {

            NetworkManager.RecursiveDelete(ConfigurationFile);

    }

    public static final void GoToSleep(long Duration) {

        long Delay = System.currentTimeMillis() + Duration;

        while (System.currentTimeMillis() < Delay) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException Ex) {
                
            }
        }

    }

    public static void copyElements(StructuredDocument intoDoc, Element intoElement, Element from, boolean recursive, boolean copyAttributes) {

        // Copying current level element
        Element newElement = intoDoc.createElement(from.getKey(), from.getValue());
        intoElement.appendChild(newElement);

        // Copy attributes (eventually)
        if (copyAttributes) {

            if ((from instanceof Attributable) && (newElement instanceof Attributable)) {

                Enumeration eachAttrib = ((Attributable) from).getAttributes();

                while (eachAttrib.hasMoreElements()) {
                    Attribute anAttrib = (Attribute) eachAttrib.nextElement();
                    ((Attributable) newElement).addAttribute(anAttrib.getName(), anAttrib.getValue());
                }

            }

        }

        // Looping through the child elements (eventually)
        if (recursive) {

            for (Enumeration eachChild = from.getChildren(); eachChild.hasMoreElements();) {

                // recurse to add the children.
                copyElements(intoDoc, newElement, (Element) eachChild.nextElement(), recursive, copyAttributes);

            }

        }

    }
}